package com.handyshop.mall.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.handyshop.mall.basic.core.vo.PageBean;
import com.handyshop.mall.basic.core.vo.PageVo;
import com.handyshop.mall.basic.core.vo.ResultVo;
import com.handyshop.mall.dubbo.enums.OrgTypeEnum;
import com.handyshop.mall.dubbo.user.dto.org.SysOrgAddDto;
import com.handyshop.mall.dubbo.user.dto.org.SysOrgEditDto;
import com.handyshop.mall.dubbo.user.entity.SysOrg;
import com.handyshop.mall.dubbo.user.entity.SysUser;
import com.handyshop.mall.user.service.SysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author GLP
 * @date 2020/11/19
 */
@RestController
@RequestMapping("/sysOrg")
@RequiredArgsConstructor
@Api(tags = "组织部门模块")
public class SysOrgController {

    private final SysOrgService sysOrgService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询部门")
    public ResultVo<SysOrg> getById(@PathVariable Long id) {
        return ResultVo.success(sysOrgService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "添加部门")
    //@PreAuthorize("@pms.hasPermission('sys_org_add')")
    public ResultVo<Boolean> save(@Validated @RequestBody SysOrgAddDto sysOrgAddDto) {
        SysOrg sysOrg = BeanUtil.copyProperties(sysOrgAddDto, SysOrg.class);
        sysOrg.setType(OrgTypeEnum.ORGANIZATION.getId());
        return ResultVo.success(sysOrgService.save(sysOrg));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    //@PreAuthorize("@pms.hasPermission('sys_org_del')")
    public ResultVo<Void> removeById(@PathVariable Long id) {
        return sysOrgService.delById(id);
    }

    @PutMapping
    @ApiOperation(value = "根据id编辑")
    //@PreAuthorize("@pms.hasPermission('sys_org_edit')")
    public ResultVo<Boolean> update(@Validated @RequestBody SysOrgEditDto sysOrgEditDto) {
        SysOrg sysOrg = BeanUtil.copyProperties(sysOrgEditDto, SysOrg.class);
        return ResultVo.success(sysOrgService.updateById(sysOrg));
    }

    @GetMapping("/findOrgUser/{id}")
    @ApiOperation(value = "通过ID查询部门用户")
    public ResultVo<PageVo<SysUser>> findOrgUserById(PageBean pageBean, @PathVariable Long id) {
        return ResultVo.success(sysOrgService.findOrgUserById(pageBean, id));
    }

}
